﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Threading;

namespace SCOSub
{
    public partial class VisualStaffMaker_VideoPlayer : Form, IGetInfo
    {
        public delegate void PlayerCloseHandle();
        public delegate void SimpleDelegate();

        //public event PlayerCloseHandle PlayClose;

        private AVSVideoProvider avsp;
        /*
        private IntPtr avs = IntPtr.Zero;
        private AVSDLLVideoInfo vi;
        private AviSynthColorspace ac;
        private AudioSampleType ast;
        Bitmap b;
        /// <summary>
        /// 除数
        /// </summary>
        private int rated; 
        /// <summary>
        /// 被除数
        /// </summary>
        private int raten;
        /// <summary>
        /// 正在播放
        /// </summary>
        private bool isplaying = false;

     

        private int currentframe;
        /// <summary>
        /// 当前帧
        /// </summary>
        public int CurrentFrame
        {
            get { return currentframe; }
        }
        */
        public VisualStaffMaker_VideoPlayer()
        {
            InitializeComponent();
        }
        /* public VisualStaffMaker_VideoPlayer(string filename)
         {
             InitializeComponent();
             avsp = new AVSVideoProvider();
             avsp.loadAVS(filename);

             trackBar1.Maximum = avsp.vi.num_frames;
             trackBar1.Minimum = 0;
             trackBar1.Value = 0;
             trackBar1.TickFrequency = avsp.vi.num_frames / 1000;



             this.Width = avsp.vi.width + 6;
             this.Height = avsp.vi.height + 74;
            
         }
         * */
        public void OpenFile(string filename)
        {
            avsp = new AVSVideoProvider();
            avsp.loadAVS(filename);

            trackBar1.Maximum = avsp.vi.num_frames - 1;
            trackBar1.Minimum = 0;
            trackBar1.Value = 0;
            trackBar1.TickFrequency = avsp.vi.num_frames / 1000;
            this.Text = trackBar1.Value.ToString() + " / " + (avsp.vi.num_frames - 1).ToString();
            if (avsp != null)
            {
                this.Width = avsp.vi.width + 4;
                this.Height = avsp.vi.height + 72;
            }

        }
        private void VisualStaffMaker_VideoPlayer_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (avsp != null)
                avsp.closeAVS();
            //PlayClose();
        }


        private void VisualStaffMaker_VideoPlayer_Resize(object sender, EventArgs e)
        {
            /*if (b != null)
            {
                if (pictureBox1.Image != null) pictureBox1.Image.Dispose();
                pictureBox1.Image = resizeBitmap(b, pictureBox1.Width, pictureBox1.Height);
                pictureBox1.Refresh();
            }
             * */
        }
        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            if (avsp == null)
                return;
            if (pictureBox1.Image != null)
                pictureBox1.Image.Dispose();
            Bitmap b = avsp.ReadFrameBitmap(trackBar1.Value);
            if (b == null)
                return;
            pictureBox1.Image = b;
            int allms = (int)((double)trackBar1.Value * avsp.vi.rated / avsp.vi.raten * 1000);
            int h = allms / 1000 / 3600;
            int min = (allms % 3600000) / 1000 / 60;
            int s = (allms % 60000) / 1000;
            int ms = allms % 1000;
            this.Text = trackBar1.Value.ToString() + " / " + (avsp.vi.num_frames - 1).ToString() + "   " + string.Format("{0:0}:{1:00}:{2:00}.{3:000}", h, min, s, ms);
            pictureBox1.Refresh();

        }


        /*
        private void play()
        {
            while (isplaying)
            {
                if (currentframe < trackBar1.Maximum)
                    currentframe++;
                else
                    isplaying = false;
                this.trackBar1.Invoke(new SimpleDelegate(updatepicbox));
                Thread.Sleep((int)Math.Round((double)1000 * rated / raten, 0, MidpointRounding.ToEven));
                Application.DoEvents();
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.button1.Text.Equals("Play"))
            {
                this.isplaying = true;
                this.button1.Text = "Stop";
                new Thread(new ThreadStart(this.play)).Start();
            }
            else
            {
                this.isplaying = false;
                this.button1.Text = "Play";
            }
          
        }
        
        private void updatepicbox()
        {
            Text = currentframe.ToString();
            trackBar1_Scroll(null, null);
        }
        */







        #region IGetInfo 成员

        public bool getTimeTip_staff(out string n)
        {

            n = string.Format("+{0:D8}", trackBar1.Value);
            return true;

        }

        #endregion
    }
}
